package com.thprojects.myphotomemories.controller;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.util.Log;

import com.thprojects.myphotomemories.model.Contact;

public class ContactsController {
	Context mContext;
    OnGotContactsListener mOGCListener;
    
    List<Contact> mSelContacts = new ArrayList<Contact>();
    
    //elements with the same index refer to the same contact
    List<Contact> mContacts = new ArrayList<Contact>();
    String[] mContactNames;
    boolean[] mContactSelected;
    

	
	public ContactsController(Context context, OnGotContactsListener OGCListener) {
		mContext = context;
		mOGCListener = OGCListener;	
		fetchContacts();
	}
	
	public ContactsController(List<Contact> contacts, Context context, OnGotContactsListener OGListener){
		this(context, OGListener);
		for (Contact c: contacts){
			for (int i=0; i<mContacts.size(); i++){
				if (c.getLookupKey().equals(mContacts.get(i).getLookupKey())){
					mContactSelected[i] = true;
					mSelContacts.add(mContacts.get(i));
					//exit inner loop
					i = mContacts.size();
				}
			}
		}
	}
    
	public void askContacts(){

		
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle("Select some contacts");
		builder.setMultiChoiceItems(mContactNames, mContactSelected, new OnMultiChoiceClickListener() {
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				if (isChecked){
					mSelContacts.add(mContacts.get(which));
					mContactSelected[which] = true;
				}
				else{
					mSelContacts.remove(mContacts.get(which));
					mContactSelected[which] = false;
				}
			}
		});

		builder.setPositiveButton("Ok", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				mOGCListener.onGotContacts(mSelContacts);
			}
		});
		builder.setNegativeButton("Cancel", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});

		Log.d("MPM", "so far so good");
		builder.create();
		Log.d("MPM", "so far so good");
		builder.show();
	}
    
	private void fetchContacts(){
		//TODO: Async task?
		
		String[] projection = new String[]{
				ContactsContract.Contacts.LOOKUP_KEY,
				ContactsContract.Contacts.DISPLAY_NAME,
		};
		Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, projection, null, null, ContactsContract.Contacts.DISPLAY_NAME);
		try{

			while (cursor.moveToNext()){
				String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
				String key = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
				
				if (name != null) mContacts.add(new Contact(name, key));
			}
			
			cursor.close();
			
			mContactNames = new String[mContacts.size()];
			mContactSelected = new boolean[mContacts.size()];
			for (int i=0; i < mContacts.size(); i++){
				mContactNames[i] = mContacts.get(i).getDisplayName();
				mContactSelected[i] = false;
			}

		}
		catch (Exception e){
			GuiController.showError(mContext, "Error trying to get contacts. Please try again");
			Log.d("MPM", e.getMessage());
		}
		finally{
			cursor.close();
		}

	}
	
	public static String getDisplayName(Context context, String lookupKey){
		Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey);
		Cursor c = context.getContentResolver().query(lookupUri, new String[]{Contacts.DISPLAY_NAME}, null, null, null);
		try {
		    c.moveToFirst();
		    return c.getString(0);
		} finally {
		    c.close();
		}
	}
	
}
